<?php
	
class Admin_OrderController extends TN_Controller_Action_Admin
{
 public function init()
    {
    	parent::init();
    }

 	public function indexAction()
 	{
    	$content = new Zend_Models_Order();
 		$this->view->contents = $content->getAll();
 	}
 	
 	public function filterAction()
 	{
 	  $content = new Zend_Models_Order();
 	  $request = $this->_request->getParams();
 	  if ( isset($request['status']) && $request['status'] != "" )
 	    $this->view->contents = $content->getOrderByStatus($request['status']);
 	  else
 	    $this->view->contents = $content->getAll();
 	  $this->view->status = $request['status'];
 	}
 	
 	
	public function changeAction()
	{
	  parent::disableLayout();
	  $request = $this->_request->getParams();
	  $order = new Zend_Models_Order();
	  $order = $order->getOrderById($request['order']);
	  $oldState = $order->status;
	  $order->status = $request['state'];
	  $order->save();
	 
	}
 	
 	public function productsAction()
 	{
 	  parent::disableLayout();
 	  $id_order = $this->_request->getParam('id');
 	  $products = new Zend_Models_Order();
 	  $products = $products->getProductsByOrder($id_order);
 	  $this->view->products = $products;
 	}
 	
 	public function neworderAction()
 	{
	    $form = $this->addOrderForm();
 		$this->view->form = $form;	
        if (!$this->getRequest()->isPost())
     	{
     	  	return;
     	}
     	else
    	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$content = new Zend_Models_Order();
				$content = $content->createRow();
				$content->title = $values['common']['title'];
				$content->enabled = $values['common']['enabled'];
				$content->save();
				$this->_redirect("/admin/order/index");
			}
		}
	 }
	 
	private function addOrderForm()
	 {
		$form = new Zend_Form();
	 	$form->setAction("/admin/order/neworder")
	             ->setMethod('post');
    	$form->setAttrib("class", "jNice");         
        /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $common->addElement($enabled);	
 	    $title = $common->createElement("text",'title');
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$common->addElement($title);
		$form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/
	    $form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
	}
 	
 	public function editorderAction()
 	{
 		$form = $this->editOrderForm();
 		$this->view->form = $form;	
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
    	 else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$order = new Zend_Models_Order();
				$order = $order->getOrderById($values['id_order']);
				$order->enabled = $values['common']['enabled'];
				$order->title = $values['common']['title'];
				$order->save();
				$this->_redirect("/admin/order/index");
			}
		}
 	}
 	
	private function editOrderForm()
	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/order/editorder")
       	      ->setMethod('post');
    	$form->setAttrib("class", "jNice");   
    	$id_order = $this->_request->getParam('order');
    
    	$order = new Zend_Models_Order();  
    	$order = $order->getOrderById($id_order);         
         /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($order->enabled);
 	    $common->addElement($enabled);
 	    $title = $common->createElement("text","title");
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$title->setValue($order->title);
		$common->addElement($title);	
	    $form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/     
	 	$id = $form->createElement("hidden","id_order");
		$id->setValue($order->id);
    	$form->addElement($id);
     	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
 }
 
 	public function deleteorderAction()
	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$order = $request['order'];
   	 	$c = new Zend_Models_Order();
   	 	$c = $c->getOrderById($order);
    	$c->delete();
    	$this->_redirect("/admin/order/index");
 	}
}    